﻿

#region using namespace
using System.Collections.Generic;
using System.Linq;
using DTO;
using System;
#endregion

namespace DAO
{
    public class TuKhoaPhuongDAO
    {
       // tim tu khoa phuong dua vao ma tu khoa phuong.
        public List<TuKhoaPhuongDTO> Search(string strInput)
        {
            TuKhoaPhuongDTO tukhoaPhuongDTO = null;
            strInput = strInput.Replace(@"'", @"''");
            List<TuKhoaPhuongDTO> lsttukhoaphuong = getAllPhuong();

            List<TuKhoaPhuongDTO> ilsttukhoaphuong = new List<TuKhoaPhuongDTO>();
         
            foreach (TuKhoaPhuongDTO tukhoa in lsttukhoaphuong)
            {
                if (strInput.Contains(tukhoa.TuKhoaPhuong)||tukhoa.TuKhoaPhuong.Contains(strInput))
                {
                    tukhoaPhuongDTO = new TuKhoaPhuongDTO();

                    tukhoaPhuongDTO.MaTuKhoaPhuong = tukhoa.MaTuKhoaPhuong;
                    tukhoaPhuongDTO.MaPhuong = tukhoa.MaPhuong;
                    tukhoaPhuongDTO.TuKhoaPhuong = tukhoa.TuKhoaPhuong;

                    ilsttukhoaphuong.Add(tukhoaPhuongDTO);
                }
            }

            return ilsttukhoaphuong;
        }

        //lay tat ca cac thuoc tinh cua bang tu khoa phuong.
        public static List<TuKhoaPhuongDTO> getAllPhuong()
        {
            IList<TUKHOAPHUONG> ilsttkphuong;
            List<TuKhoaPhuongDTO> lsttkphuong = new List<TuKhoaPhuongDTO>();
            TuKhoaPhuongDTO tkphuongDTO = null;

            using (var context = new DataContext(GlobalDatabase.ConnectString))
            {
                ilsttkphuong = (from phuong in context.TUKHOAPHUONGs select phuong).ToList();
            }

            for (int i = 0; i < ilsttkphuong.Count; i++)
            {
                tkphuongDTO = new TuKhoaPhuongDTO();

                tkphuongDTO.MaPhuong = (int)ilsttkphuong[i].MaPhuong;
                tkphuongDTO.TuKhoaPhuong = ilsttkphuong[i].TuKhoaPhuong1;
                tkphuongDTO.MaTuKhoaPhuong = ilsttkphuong[i].MaTuKhoaPhuong;

                lsttkphuong.Add(tkphuongDTO);
            }

            return lsttkphuong;
        }
        
    }
}
